﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBM;
using IBM.WMQ;


namespace AlisV6MQ
{
    public class CAlisV6MQ
    {
        string m_sManager;
        string m_sChannel;
        string m_sServer;
        string m_sRequest;
        string m_sResponse;

        MQQueueManager mqQMgr = null;
        MQQueue requestQueue = null;
        MQMessage requestMessage = null;
        public bool Init(string sServer, string sManager, string sRequest, string sResponse, string sChannel)
        {
            bool bRet = false;
            if ((sServer != m_sServer) || (sManager != m_sManager) || (sRequest != m_sRequest) || (sResponse != m_sResponse) || (sChannel != m_sChannel)) 
            {
                m_sServer = sServer;
                m_sManager = sManager;
                m_sRequest = sRequest;
                m_sResponse = sResponse;
                m_sChannel = sChannel;

                 try
                 {
                     mqQMgr = new MQQueueManager(m_sManager, m_sChannel, m_sServer);
                     bRet = true;
                 }
                 catch (MQException mqe)
                 {
                     Console.WriteLine(mqe.ToString());
                 }
            }
            return bRet;
         }

        public string SendAndGetMessage(string sSend)
        {
            string sRet = "";

            requestQueue = mqQMgr.AccessQueue(m_sRequest, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);
            requestMessage = new MQMessage();

            requestMessage.Format = MQC.MQFMT_STRING;
            requestMessage.MessageId = MQC.MQMI_NONE;
            requestMessage.CharacterSet = 437;
            requestMessage.WriteString(sSend);
            requestMessage.CorrelationId = ConvertToBinary("416c69733030303000000000000000");
            requestMessage.GroupId = ConvertToBinary("54657374");
            requestQueue.Put(requestMessage);
            requestQueue.Close();

            MQQueue responseQueue = mqQMgr.AccessQueue(m_sResponse, MQC.MQOO_INPUT_SHARED + MQC.MQOO_FAIL_IF_QUIESCING);
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            gmo.Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_WAIT;
      
            gmo.WaitInterval = 30000;

            gmo.MatchOptions = MQC.MQMO_MATCH_MSG_ID + MQC.MQMO_MATCH_CORREL_ID;
            MQMessage message = new MQMessage();
            message.CharacterSet = 437;
            message.CorrelationId = ConvertToBinary("416c69733030303000000000000000");
            message.GroupId = ConvertToBinary("54657374");
            message.MessageId = requestMessage.MessageId;

            try
            {
                responseQueue.Get(message, gmo);
                sRet = message.ReadString(message.MessageLength);

            }
            catch (MQException mqe)
            {
                Console.WriteLine(mqe.ToString());
            }
            responseQueue.Close();

            return sRet;
        }

        public string  displayQueueDepth()
        {
            //MQEnvironment.Hostname = m_sServer;
            //MQEnvironment.Port = 1414;
            //MQEnvironment.Channel = m_sChannel;
            MQQueueManager manager = new MQQueueManager(m_sManager, m_sChannel, m_sServer); ;
            MQQueue mqQueue1 = manager.AccessQueue(m_sRequest,
                    MQC.MQOO_INPUT_AS_Q_DEF                    // open queue for input
                    + MQC.MQOO_FAIL_IF_QUIESCING               // but not if MQM stopping
                    + MQC.MQOO_INQUIRE);                       // and we need to inquire
            MQQueue mqQueue2 = manager.AccessQueue(m_sResponse,
                    MQC.MQOO_INPUT_AS_Q_DEF                    // open queue for input
                    + MQC.MQOO_FAIL_IF_QUIESCING               // but not if MQM stopping
                    + MQC.MQOO_INQUIRE);                       // and we need to inquire

            //m_sServer = sServer;
            return mqQueue1.CurrentDepth.ToString() + "." + mqQueue2.CurrentDepth.ToString();
        }

        public static Byte[] ConvertToBinary(string newString)
        {
            int byteLength = newString.Length / 2;
            byte[] bytes = new byte[byteLength];
            string hex;
            int j = 0;
            for (int i = 0; i < bytes.Length; i++)
            {
                hex = new String(new Char[] {newString[j],
												newString[j+1]});
                bytes[i] = byte.Parse(hex,
                    System.Globalization.NumberStyles.HexNumber);
                j = j + 2;
            }
            return bytes;
        }

    }
}
